package cn.com.dashihui.api.ITask;

import java.util.List;

import com.jfinal.aop.Duang;

import cn.com.dashihui.api.common.OrderCode;
import cn.com.dashihui.api.dao.Order;
import cn.com.dashihui.api.dao.OrderE;
import cn.com.dashihui.api.dao.OrderG;
import cn.com.dashihui.api.dao.User;
import cn.com.dashihui.api.service.OrderService;

public class ReciveTask implements Runnable {
	
	//使用Duang.duang进行封装，使普通类具有事务的功能
	private OrderService service = Duang.duang(OrderService.class);
	
	public void run() {
		
		System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL执行直接签收开始："+System.currentTimeMillis());
		//查询普通订单
		String sql = " SELECT o.* FROM  ";
		String sql1 = " t_bus_order ";
		String sql2 = " t_bus_order_gb ";
		String sql3 = " t_bus_order_act " ;
		String sql4 = " o RIGHT JOIN t_bus_order_logistics l ON o.orderNum=l.orderCode "
				+ " WHERE o.orderState=?  AND l.charset=? ";
		
		List<Order> find = Order.me().find(sql+sql1+sql4,OrderCode.OrderState.SHOU,"0000");
		List<OrderE> findE = OrderE.me().find(sql+sql3+sql4,OrderCode.OrderState.SHOU,"0000");
		List<OrderG> findG = OrderG.me().find(sql+sql2+sql4,OrderCode.OrderState.SHOU,"0000");
		//普通订单
		System.out.println(">>>>>>>>>>>"+find);
		if (find!=null && find.size()>0) {
			System.out.println(">>>>>>>>>>>"+find);
			find.stream().forEach(order->{
				User user = User.me().findById(order.getInt("userid"));
				//3.签收订单操作，并记录日志
				try {
					//在service中进行订单签收(修改订单状态、商品返利、用户是否升级为黄金会员)的一系列操作,保证事务回滚
					service.receiveOrder(order,user);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		}
		//兑换订单
		System.out.println(">>>>>>>>>>>"+findE);
		if (findE!=null && findE.size()>0) {
			System.out.println(">>>>>>>>>>>"+findE);
			findE.stream().forEach(order->{
				User user = User.me().findById(order.getInt("userid"));
				//3.签收订单操作，并记录日志
				try {
					//在service中进行订单签收(修改订单状态、商品返利、用户是否升级为黄金会员)的一系列操作,保证事务回滚
					service.receiveExchangeOrder(order,user);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		}
		//普通订单
		System.out.println(">>>>>>>>>>>"+findG);
		if (findG!=null && findG.size()>0) {
			System.out.println(">>>>>>>>>>>"+findG);
			findG.stream().forEach(order->{
				User user = User.me().findById(order.getInt("userid"));
				
				//3.签收订单操作，并记录日志
				try {
					//在service中进行订单签收(修改订单状态、商品返利、用户是否升级为黄金会员)的一系列操作,保证事务回滚
					service.receiveOrderG(user,order.getStr("orderNum"));
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		}
		
		System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL执行直接签收结束"+System.currentTimeMillis());
	    

	}
}
